package services

import "TaskAssignmentSystem/internal/admin/dao"

type SystemOrderProductSnapshotService struct {
}

func (s *SystemOrderProductSnapshotService) SaveSnapshot(orderId string) error {
	var orderService SystemOrderService
	//查询订单
	order, err := orderService.OrderInfo(orderId)
	if err != nil {
		return err
	}
	//查产品
	var productService SystemProductService
	prod, err := productService.ProductFindByProductId(order.ProductId)
	if err != nil {
		return err
	}
	//保存快照
	var snapshot dao.SystemOrderProductSnapshot
	snapshot.OrderId = orderId
	snapshot.ProductId = prod.ProductId
	snapshot.ProductName = prod.ProductName
	snapshot.ProductType = prod.ProductType
	snapshot.ProductPrice = prod.ProductPrice
	snapshot.ProductNumber = prod.ProductNumber
	snapshot.ProductDetailPath = prod.ProductDetailPath
	snapshot.ProductPicturePath = prod.ProductPicturePath
	snapshot.Status = prod.Status
	var common dao.Common[dao.SystemOrderProductSnapshot]
	return common.CreateOne(snapshot).Error
}

func (s *SystemOrderProductSnapshotService) SnapshotFindByProductId(orderId, productId string) (result *dao.SystemOrderProductSnapshot, err error) {
	var common dao.Common[dao.SystemOrderProductSnapshot]
	if data, err := common.First(map[string]interface{}{"order_id": orderId, "product_id": productId}); err != nil {
		return nil, err
	} else {
		return &data, nil
	}
}
